package epam.lab.spring.context;

import epam.lab.spring.beanfactory.BeanFactory;
import epam.lab.spring.beanfactory.StandardBeanFactory;
import epam.lab.spring.parser.ParserTypes;
import epam.lab.spring.parser.BeanDefinition;
import epam.lab.spring.reader.BeanDefinitionReader;
import epam.lab.spring.reader.XmlBeanDefinitionReader;

import java.util.Map;

/**
 * Custom Application Context
 * @author Iurii Miedviediev
 * @version 1.0 Build 21.08.2014
 */
public class GenericXmlApplicationContext implements ApplicationContext {

    private String xmlFile;
    private BeanDefinitionReader reader;
    private BeanFactory factory;
    Map<String, BeanDefinition> beanDefinitions;

    public GenericXmlApplicationContext() {
        reader = new XmlBeanDefinitionReader(this);
    }

    @Override
    public void setParserType(ParserTypes parserType) {
        reader.setParserType(parserType);
    }

    @Override
    public void load(String xmlFileLocation){
        this.xmlFile = xmlFileLocation;
    }

    @Override
    public BeanFactory getBeanFactory() {
        if(factory == null) {
            reader.loadBeanDefinitions(xmlFile);
            factory = new StandardBeanFactory(beanDefinitions);
        }
        return factory;
    }

    @Override
    public void setBeanDefinitions(Map<String, BeanDefinition> beanDefs) {
        this.beanDefinitions = beanDefs;
    }
}